package com.wlzg.collectiontool.util

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.util.Log
import android.view.Gravity
import android.view.ViewGroup
import android.widget.FrameLayout
import com.bigkoo.pickerview.builder.OptionsPickerBuilder
import com.bigkoo.pickerview.listener.OnOptionsSelectListener
import com.bigkoo.pickerview.view.OptionsPickerView
import com.wlzg.collectiontool.model.bean.LoginRes
import java.util.*

class QuyuPickerUtil(var context: Context,var ls: (quyuid:String?,quyu:String?)->Unit) {
     var pvQuyu: OptionsPickerView<Any>? = null
    private val options1Items :List<LoginRes.SysAllDictItems.OlFormBizType> = MConfigUtil.provinceList
    private val options2Items :List<ArrayList<LoginRes.SysAllDictItems.OlFormBizType>> = MConfigUtil.cityList
    private val options3Items :List<ArrayList<ArrayList<LoginRes.SysAllDictItems.OlFormBizType>>> = MConfigUtil.townList

    init {
        initTvpicker()
    }



     fun initTvpicker() {
         pvQuyu = OptionsPickerBuilder(context,
            OnOptionsSelectListener { options1, options2, options3, v -> //返回的分别是三个级别的选中位置
                val opt1tx = if (options1Items.size > 0) options1Items.get(options1)
                    .getPickerViewText() else ""
                val opt2tx = if (options2Items.size > 0
                    && options2Items.get(options1).size > 0
                ) options2Items.get(options1).get(options2) else ""
                val opt3tx =
                    if (options2Items.size > 0 && options3Items.get(options1).size > 0 && options3Items.get(
                            options1
                        ).get(options2).size > 0
                    ) options3Items.get(options1).get(options2).get(options3) else ""
                val tx = "$opt1tx/$opt2tx/$opt3tx"


                val id1 = if (options1Items.size > 0) options1Items.get(options1)
                    .value else ""
                val id2 = if (options2Items.size > 0
                    && options2Items.get(options1).size > 0
                ) options2Items.get(options1).get(options2).value else ""
                val id3 =
                    if (options2Items.size > 0 && options3Items.get(options1).size > 0 && options3Items.get(
                            options1
                        ).get(options2).size > 0
                    ) options3Items.get(options1).get(options2).get(options3).value else ""



                val qyid="$id1,$id2,$id3"
               ls(qyid,tx)
            })
             .isDialog(true)
            .setTitleText("区域选择")
            .setDividerColor(Color.BLACK)
            .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
            .setContentTextSize(20)
            .build<Any>()
         if(options1Items.size>0&& options2Items.isNotEmpty() &&options3Items.size>0){
             pvQuyu?.setPicker(options1Items, options2Items, options3Items) //三级选择器
         }else{
             pvQuyu=null
         }

         val mDialog: Dialog = pvQuyu!!.getDialog()
         if (mDialog != null) {
             val params = FrameLayout.LayoutParams(
                 ViewGroup.LayoutParams.MATCH_PARENT,
                 ViewGroup.LayoutParams.WRAP_CONTENT,
                 Gravity.BOTTOM
             )
             params.leftMargin = 0
             params.rightMargin = 0
             pvQuyu!!.getDialogContainerLayout().setLayoutParams(params)
             val dialogWindow = mDialog.window
             if (dialogWindow != null) {
                 dialogWindow.setWindowAnimations(com.bigkoo.pickerview.R.style.picker_view_slide_anim) //修改动画样式
                 dialogWindow.setGravity(Gravity.BOTTOM)
                 dialogWindow.setDimAmount(0.3f)
             }
         }
    }
}